﻿var fs = require("fs"),
    path = require("path"),
    newsJsonPath = path.join(global.rootPath, "/assets/json/news.json");

var NewsModel = function(_title, _author, _source, _sourceUrl, _img,_introduce, _content) {

    this.id = "";
    this.title = _title || "";
    this.author = _author || "";
    this.source = _source || "";
    this.sourceUrl = _sourceUrl || "";
    this.img = _img || "";
    this.date = new Date();
    this.introduce= _introduce || "";
    this.content = _content || "";
};

NewsModel.prototype = {

    add: function(_callBack) {

        var newsList = global.news;
        this.id = this.getNewsID();
        newsList.unshift(this.getNewsAttr());
        saveJson(_callBack);
    },
    del: function(_id, _callBack) {

        var newsList = global.news,
            i = newsList.length - 1;
        for (i; i >= 0; i--) {
            if (newsList[i].id == _id) break;
        }
        if (i >= 0) {
            newsList.splice(i, 1);
            saveJson(_callBack);
        } else {
            _callBack({
                result: false
            });
        }
    },
    dels: function(_ids, _callBack) {

        var newsList = global.news;
        var temp = [];
        newsList.forEach(function(value, index) {
            if (_ids.indexOf(value.id + ";") >= 0) temp.push(index);
        });
        if (temp.length > 0) {
            temp.forEach(function(value, index) {
                newsList.splice(value - index, 1);
            });
            saveJson(_callBack);
        } else {
            _callBack({
                result: false
            });
        }
    },
    edit: function(_id, _callBack) {

        var newsList = global.news,
            i = newsList.length - 1;
        for (i; i >= 0; i--) {
            if (newsList[i].id == _id) break;
        }
        if (i >= 0) {
            newsList[i].title = this.title;
            newsList[i].author = this.author;
            newsList[i].source = this.source;
            newsList[i].sourceUrl = this.sourceUrl;
            if (this.img) newsList[i].img = this.img;
            newsList[i].introduce = this.introduce;
            newsList[i].content = this.content;
            saveJson(_callBack);
        } else {
            _callBack({
                result: false
            });
        }
    },
    select: function(_id) {

        var newsList = global.news,
            i = newsList.length - 1;
        for (i; i >= 0; i--) {
            if (newsList[i].id == _id) break;
        };
        console.log(newsList[i]);
        return i >= 0 ? newsList[i] : null;
    },
    getNewsAttr: function() {

        return {
            id: this.id,
            title: this.title,
            author: this.author,
            source: this.source,
            sourceUrl: this.sourceUrl,
            createDate: this.date.format("yyyy-MM-dd hh:mm"),
            img: this.img,
            introduce:this.introduce,
            content: this.content
        };
    },
    getNewsID: function() {

        var date = new Date();
        return "" + date.getFullYear() + date.getMonth() + date.getDay() + date.getHours() + date.getMinutes() + date.getSeconds();
    }
};

function saveJson(_callBack) {

    fs.writeFile(newsJsonPath, JSON.stringify(global.news, null, "\t"), function(err) {
        if (err) {
            console.log(err);
            if (_callBack) _callBack({
                result: false
            });
        }
        if (_callBack)
            _callBack({
                result: true
            });
    });
};

module.exports = NewsModel;